package cn.itcast.mq;

import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Component;

import javax.xml.validation.Validator;
import java.util.Map;

/**
 * @author ：xiaoZhou
 * @ClassName : SpringRabbitListener
 * @date ：Created in 2021/12/14 17:22
 *  接受消息的类
 */

@Component  //将该类作为bean对象加载到ioc容器中
public class SpringRabbitListener {

//    /**
//     *  接受 simple.queue 传递消息的方法
//     * @param massage  传递过来的消息
//     */
//    @RabbitListener(queues = "simple.queue")
//    public  void  listenSimpleQueueMessage(String massage){
//        System.out.println("massage = " + massage);
//    }

    /**
     *  接受 simple.queue 传递消息的方法   一对多
     * @param massage  传递过来的消息
     */
    @RabbitListener(queues = "simple.queue")
    public  void  listenWorkQueueMessage(String massage) throws InterruptedException {
        System.out.println("接受者1 = " + massage);
        //加上休眠方便测试
        Thread.sleep(20);
    }
    @RabbitListener(queues = "simple.queue")
    public  void  listenWorkQueueMessage2(String massage) throws InterruptedException {
        System.out.println("结接受者 === = " + massage);
        Thread.sleep(50);
    }


    /**
     * 接收订阅 广播的消息  群发
     */
    @RabbitListener(queues = "fanout.queue1")
    public void listenFanoutQueue1(String massage){
        System.out.println("接收者1" + massage);
    }
    @RabbitListener(queues = "fanout.queue2")
    public void listenFanoutQueue2(String massage){
        System.out.println("接收者2" + massage);
    }


    /**
     * 接收  路由模式
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue1"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"luyou","luyou1"}
    ))
    public void listenDirectQueue1(String massage){
        System.out.println("消费者 1 " + massage );
    }

    /**
     * 接收者2
     * @param massage
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "direct.queue2"),
            exchange = @Exchange(name = "itcast.direct",type = ExchangeTypes.DIRECT),
            key = {"luyou","luyou2"}
    ))
    public void listenDirectQueue2(String massage){
        System.out.println("消费者 2 " + massage );
    }





    /**
     * 接收者1   topic 主题
     * @param massage
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue1"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"china.#"}
    ))
    public void listenTopicQueue1(String massage){
        System.out.println("消费者 1 " + massage );
    }
    /**
     * 接收者2   topic 主题
     * @param massage
     */
    @RabbitListener(bindings = @QueueBinding(
            value = @Queue(name = "topic.queue2"),
            exchange = @Exchange(name = "itcast.topic",type = ExchangeTypes.TOPIC),
            key = {"#.news"}
    ))
    public void listenTopicQueue2(String massage){
        System.out.println("消费者2 " + massage );
    }


    /**
     * 接收 jdk转化器发送的数据
     */
    @RabbitListener(queues = "itcast.queue")
    public void test(Map<String,String> map){
        System.out.println(map);
    }




}
